<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>键盘文字输入监听</title>
</head>
<style>
    textarea{
        width: 200px;
        height: 70px;
        resize: none;
        text-align: justify;
    }
</style>
<body>
    <textarea name="" id=""></textarea>
    <span>0/50</span>
    <script>
        var text = document.querySelector("textarea");
        var span = document.querySelector("span");
        text.addEventListener("input",function(){
            if(text.value.length >= 51){
                alert("超过字数");
                text.maxLength="50";
            }else{
                span.innerHTML =`${text.value.length}/50`;
            }
        })

    </script>
</body>
</html>